package com.security.door.controller.baseManage;

import com.security.door.core.wrapper.WrapMapper;
import com.security.door.core.wrapper.Wrapper;
import com.security.door.model.domain.baseMenu;
import com.security.door.model.domain.baseUserRole;
import com.security.door.model.dto.baseMenuDto;
import com.security.door.model.vo.menuListVo;
import com.security.door.service.IbaseDatadictService;
import com.security.door.service.IbaseMenuService;
import com.security.door.service.IbaseUserRoleService;
import com.security.door.service.IbaseUsersService;
import com.security.door.utils.HttpRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.websocket.server.PathParam;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@Api(value = "WEB - baseMenuController", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class baseMenuController {

    @Autowired
    HttpSession session;
    @Autowired
    HttpServletRequest httpServletRequest;
    @Autowired
    IbaseMenuService ibaseMenuService;
    @Autowired
    IbaseUserRoleService ibaseUserRoleService;
    @Autowired
    IbaseDatadictService ibaseDatadictService;
    @Autowired
    IbaseUsersService ibaseUsersService;

    @GetMapping(value = "/baseMenu/menus")
    @ApiOperation(value = "根据角色查询所有菜单数据", notes = "根据角色查询所有菜单数据")
    public Wrapper MenuList(@PathParam("token") String token) {
        var baseUser = ibaseUsersService.selectByToken(token);

        if (null != baseUser) {
            var menus = ibaseMenuService.selectByUserId(baseUser.getGuid());
            return WrapMapper.ok(getTree(menus, 0));
        }
        return WrapMapper.error();
    }

    @GetMapping(value = "/baseMenu/List")
    @ApiOperation(value = "根据角色查询所有菜单数据", notes = "根据角色查询所有菜单数据")
    public Wrapper AttachList() {
        List<baseMenu> treeData = null;
        List<baseUserRole> list = ibaseUserRoleService.selectRoleByUserId("0faa2198-65a1-47ff-9ffb-a3c058853016");
        String roleCodes = null;
        for (var temp : list) {
            roleCodes = (null == roleCodes) ? temp.getRolecode() : (roleCodes += "," + temp.getRolecode());
        }
        Map<String, Object> map = new HashMap<>();
        map.put("rolecode", roleCodes);
        map.put("index", 0);
        treeData = ibaseMenuService.selectByRoleCode(map);
        return WrapMapper.ok(getTree(treeData, 0));
    }

    @GetMapping(value = "/baseMenu/getAllTreeList")
    @ApiOperation(value = "查询所有菜单数据", notes = "查询所有菜单数据")
    public Wrapper getAllTreeList() {
        List<baseMenu> list = ibaseMenuService.selectAll();
        return WrapMapper.ok(getTree(list, 0));
    }

    private List<menuListVo> getTree(List<baseMenu> list, int parentId) {
        List<baseMenu> listChildrens = null;
        listChildrens = list.stream().filter(x -> x.getParentid() == parentId).collect(Collectors.toList());
        List<menuListVo> listVo = new ArrayList<menuListVo>();
        for (var children : listChildrens) {
            menuListVo vo = new menuListVo();
            vo.setGuid(children.getGuid());
            vo.setMenuid(children.getMenuid());
            vo.setMenutype(children.getMenutype());
            vo.setTitle(children.getMenuname());
            vo.setMenuname(children.getMenuname());
            vo.setIcon(children.getIcon());
            vo.setHref(children.getActionname());
            vo.setActionname(children.getActionname());
            vo.setParentid(children.getParentid());
            vo.setNum(children.getMenuid());
            vo.setName(children.getMenuname());
            vo.setClosable(true);
            vo.setShowInTags(false);
            vo.setShowInMenus(children.getActive());
            vo.setChoosed(false);
            vo.setActive(children.getActive());
            vo.setRemark(children.getRemark());

            var tempList = getTree(list, children.getMenuid());
            if (tempList.size() > 0)
                vo.setChildren(tempList);
            listVo.add(vo);
        }
        return listVo;
    }

    @GetMapping(value = "/baseMenu/selectAll")
    @ApiOperation(value = "查询所有菜单数据", notes = "查询所有菜单数据")
    public Wrapper selectAll() {
        List<baseMenu> list = ibaseMenuService.selectAll();
        return WrapMapper.ok(list);
    }

    @PostMapping(value = "/baseMenu/Add")
    public Wrapper insert(@RequestBody baseMenu entity) {
        if ("".equals(entity.getGuid()) || null == entity.getGuid()) {
            entity.setGuid(UUID.randomUUID().toString());
            if (null == entity.getParentid())
                entity.setParentid(0);
            Integer menuId = ibaseMenuService.getMenuId(entity.getParentid());
            if (null == menuId)
                menuId = entity.getParentid() * 100;
            menuId += 1;
            entity.setMenuid(menuId);
            entity.setSortno(menuId);
            entity.setCreatetime(new Date());
            ibaseMenuService.save(entity);
        } else {
            ibaseMenuService.update(entity);
        }
        return WrapMapper.ok("操作成功");
    }

    @GetMapping(value = "/baseMenu/Delete/{guid}")
    public Wrapper deleteByGuid(@PathVariable("guid") String guid) {
        int result = ibaseMenuService.deleteByKey(guid);
        String resultStr = result == 0 ? "数据不存在" : "操作成功";
        return WrapMapper.ok(resultStr);
    }

    @GetMapping(value = "/baseMenu/updateState/{guid}")
    public Wrapper updateByGuid(@PathVariable("guid") String guid) {
        int result = ibaseMenuService.updateState(guid);
        return WrapMapper.ok(result);
    }
}
